package com.googlecode.cardamomo.ermodel;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.googlecode.cardamomo.jaxb.BooleanAdapter;

@XmlType(namespace=DataType.CARDAMOMO_NAMESPACE,
		propOrder={"order","columnName","descending"})

public class Key {

	private int order;
	private String columnName;
	private boolean descending;
	
	public Key()
	{
	}
	
	public Key(int order, String name, boolean asc)
	{
		this.order = order;
		columnName = name;
		descending = !asc;
	}

	@XmlAttribute(required=true)
	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
//		System.err.println("Key" +columnName+ ".setOrder "+order);
		this.order = order;
	}

	@XmlAttribute(name="columnName",required=true)
	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
//		System.err.println("Key.setColumnName "+columnName);
		this.columnName = columnName;
	}

	public void setDescending(Boolean descending) {
		this.descending = descending;
	}

	@XmlAttribute(name="descending")
	@XmlJavaTypeAdapter(type=Boolean.class, value = BooleanAdapter.class)
	public Boolean isDescending() {
		return descending;
	}

	@Override
	public String toString()
	{
		return "Key "+columnName+"["+order+"] ";
	}
	
	@Override
	public boolean equals(Object obj) {
		Key km=(Key) obj;
		return (km.getColumnName().equals(getColumnName()) &&
				km.isDescending() == isDescending());
	}

}
